package com.wmx.reddoor.jpa.controller;

import com.wmx.reddoor.jpa.dao.StudentRepository;
import com.wmx.reddoor.jpa.entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 学生控制器层
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2023/5/21 17:13
 */
@RestController
public class StudentController {

    @Resource
    private StudentRepository studentRepository;

    /**
     * 分页查询
     * http://localhost:8080/student/listPage
     * http://localhost:8080/student/listPage?pageNo=1&pageSize=2
     *
     * @param pageNo   ：页码，从1开始
     * @param pageSize ：每页展示的条数
     * @return
     */
    @GetMapping("/student/listPage")
    public Page<Student> listPage(@RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "50") Integer pageSize) {
        Pageable pageRequest = PageRequest.of(pageNo - 1, pageSize);
        Page<Student> studentPage = studentRepository.findAll(pageRequest);
        return studentPage;
    }
}
